package com.zone.vehicle.superapp.view.login.setSecret;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.zone.vehicle.superapp.databinding.FragmentSendMessageBinding;
import com.zone.vehicle.superapp.view.common.base.BaseFragment;
import com.zone.vehicle.superapp.view.common.enums.ActionEnum;
import com.zone.vehicle.superapp.view.common.listener.ActionListener;
import com.zone.vehicle.superapp.view.login.SetCodeFragment;

public class ForgetSecretFragment extends BaseFragment {

    private FragmentSendMessageBinding binding;

    private FragmentPagerAdapter adapter;

    private ResetSecretFragment resetSecretFragment;
    private SetCodeFragment setCodeFragment;

    public static final String RESET_SECRET_FRAGMENT = "ResetSecretFragment";
    public static final String SET_CODE_FRAGMENT = "SetCodeFragment";

    public ForgetSecretFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        addListener(listener);
    }

    @Override
    public void initFragmentPages() {
        resetSecretFragment = new ResetSecretFragment();
        setCodeFragment = new SetCodeFragment();
        addPage(resetSecretFragment);
        addPage(setCodeFragment);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentSendMessageBinding.inflate(inflater);
        binding.viewpager.setAdapter(adapter);
        return binding.controller;
    }

    private ActionListener listener = new ActionListener() {
        @Override
        public boolean onAction(ActionEnum action) {
            Log.e(TAG, "onAction="+ action);
            return false;
        }

        @Override
        public boolean onPageChanged(String pageName) {
            Log.e(TAG, "onPageChanged="+ pageName);
            switch (pageName){
                case RESET_SECRET_FRAGMENT:
                    binding.viewpager.setCurrentItem(0);
                    return true;
                case SET_CODE_FRAGMENT:
                    setCodeFragment.setPhone(resetSecretFragment.getPhone());
                    setCodeFragment.setCaptchaCertificate(resetSecretFragment.getCaptchaCertificate());
                    binding.viewpager.setCurrentItem(1);
                    return true;
            }
            return false;
        }
    };

    private void init(){
        adapter = new FragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public int getCount() {
                return getPages().size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return getPage(position);
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeListener(listener);
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.viewpager.setCurrentItem(0);
    }
}